package com.xt.base.dao.hibernate.userType;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Utility class for accessing and manipulating {@link Enum}s.
 * </p>
 * 
 * @author ��� <br>
 * @version $Revision: 1407 $<br>
 *          $Id: EnumUtils.java 1407 2006-03-24 02:57:22Z yangkaifeng $
 */
public class EnumUtils {

	/**
	 * Public constructor. This class should not normally be instantiated.
	 * 
	 * @since 2.0
	 */
	public EnumUtils() {
	}

	/**
	 * <p>
	 * Gets an <code>Enum</code> object by class and name.
	 * </p>
	 * 
	 * @param enumClass
	 *            the class of the <code>Enum</code> to get
	 * @param name
	 *            the name of the Enum to get, may be <code>null</code>
	 * @return the enum object
	 * @throws IllegalArgumentException
	 *             if the enum class is <code>null</code>
	 */
	public static Enum getEnum(Class<? extends Enum> enumClass, String name) {
		return Enum.getEnum(enumClass, name);
	}

	/**
	 * <p>
	 * Gets a <code>ValuedEnum</code> object by class and value.
	 * </p>
	 * 
	 * @param enumClass
	 *            the class of the <code>Enum</code> to get
	 * @param value
	 *            the value of the <code>Enum</code> to get
	 * @return the enum object, or null if the enum does not exist
	 * @throws IllegalArgumentException
	 *             if the enum class is <code>null</code>
	 */
	public static Enum getEnum(Class<? extends Enum> enumClass, int value) {
		return Enum.getEnum(enumClass, value);
	}

	/**
	 * <p>
	 * Gets the <code>Map</code> of <code>Enum</code> objects by name using
	 * the <code>Enum</code> class.
	 * </p>
	 * 
	 * <p>
	 * If the requested class has no enum objects an empty <code>Map</code> is
	 * returned. The <code>Map</code> is unmodifiable.
	 * </p>
	 * 
	 * @param enumClass
	 *            the class of the <code>Enum</code> to get
	 * @return the enum object Map
	 * @throws IllegalArgumentException
	 *             if the enum class is <code>null</code>
	 * @throws IllegalArgumentException
	 *             if the enum class is not a subclass of <code>Enum</code>
	 */
	public static Map<?, ?> getEnumMap(Class<? extends Enum> enumClass) {
		return Enum.getEnumMap(enumClass);
	}

	/**
	 * <p>
	 * Gets the <code>List</code> of <code>Enum</code> objects using the
	 * <code>Enum</code> class.
	 * </p>
	 * 
	 * <p>
	 * The list is in the order that the objects were created (source code
	 * order).
	 * </p>
	 * 
	 * <p>
	 * If the requested class has no enum objects an empty <code>List</code>
	 * is returned. The <code>List</code> is unmodifiable.
	 * </p>
	 * 
	 * @param enumClass
	 *            the class of the Enum to get
	 * @return the enum object Map
	 * @throws IllegalArgumentException
	 *             if the enum class is <code>null</code>
	 * @throws IllegalArgumentException
	 *             if the enum class is not a subclass of <code>Enum</code>
	 */
	public static List<?> getEnumList(Class<? extends Enum> enumClass) {
		return Enum.getEnumList(enumClass);
	}

	/**
	 * <p>
	 * Gets an <code>Iterator</code> over the <code>Enum</code> objects in
	 * an <code>Enum</code> class.
	 * </p>
	 * 
	 * <p>
	 * The iterator is in the order that the objects were created (source code
	 * order).
	 * </p>
	 * 
	 * <p>
	 * If the requested class has no enum objects an empty <code>Iterator</code>
	 * is returned. The <code>Iterator</code> is unmodifiable.
	 * </p>
	 * 
	 * @param enumClass
	 *            the class of the <code>Enum</code> to get
	 * @return an <code>Iterator</code> of the <code>Enum</code> objects
	 * @throws IllegalArgumentException
	 *             if the enum class is <code>null</code>
	 * @throws IllegalArgumentException
	 *             if the enum class is not a subclass of <code>Enum</code>
	 */
	public static Iterator<?> iterator(Class<? extends Enum> enumClass) {
		return Enum.getEnumList(enumClass).iterator();
	}

	/**
	 * @param enumClass
	 *            ö������
	 * @param int_value
	 *            ʹ��ö�ٽ��ж�ѡ��ݵ�intֵ����λ���
	 * @return ѡ��ѡ��ĵ�����
	 */
	public static int[] getInt2Bit(Class<? extends Enum> enumClass, int int_value) {
		List<?> el = EnumUtils.getEnumList(enumClass);
		int[] result = new int[el.size()];
		int i = 0;
		for (Iterator<?> iter = el.iterator(); iter.hasNext();) {
			Enum e = (Enum) iter.next();
			if ((e.getValue() & int_value) == e.getValue()) {
				result[i] = e.getValue();
			} else
				result[i] = 0;
			i++;
		}
		return result;
	}
	
	public static String[] getNameBit(Class<? extends Enum> enumClass, int int_value){
		List<?> el = EnumUtils.getEnumList(enumClass);
		String[] result = new String[el.size()];
		int i = 0;
		for (Iterator<?> iter = el.iterator(); iter.hasNext();) {
			Enum e = (Enum) iter.next();
			if ((e.getValue() & int_value) == e.getValue()) {
				result[i] = e.getName();
			} 
			i++;
		}
		return result;
	}

	/**
	 * @param enumClass
	 *            ö������
	 * @param int_value
	 *            ʹ��ö�ٽ��ж�ѡ��ݵ�intֵ����λ���
	 * @return ѡ��ѡ��ĵ�����
	 */
	public static int getBit2Int(int[] intArray) {
		int result=0;
		if (intArray==null) return result;
		for (int j = 0; j < intArray.length; j++) {
			result=result+ intArray[j];
		}
		return result;
	}

}
